package com.example.dnm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dnm.dao.*;
import com.example.dnm.pojo.*;
import com.example.dnm.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> finduser(User user) {
        if (user.getPage() != null && user.getSize() != null) {
            PageHelper.startPage(user.getPage(), user.getSize());
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        List<User> list = userDao.selectList(userQueryWrapper);
        return list;
    }

    @Override
    public User findUserByName(User user) {
        return userDao.selectOne(new QueryWrapper<User>().eq("username", user.getUsername()));
    }

    @Override
    public int addUser(User user) {
        return userDao.insert(user);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateById(user);
    }

    @Override
    public int deleteUser(User user) {
        return userDao.deleteById(user.getId());
    }

    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleMenuDao roleMenuDao;
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<User> selectThree(User user) {
        if (user.getPage() != null && user.getSize() != null) {
            PageHelper.startPage(user.getPage(), user.getSize());
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        List<User> list = userDao.selectList(userQueryWrapper);
        for (User user1 : list) {
            UserRole userRole = userRoleDao.selectOne(new QueryWrapper<UserRole>().eq("userid", user1.getId()));
            if (userRole != null) {
                Role role = roleDao.selectById(userRole.getRoleid());
                List<RoleMenu> list1 = roleMenuDao.selectList(new QueryWrapper<RoleMenu>().eq("roleid", role.getId()));
                List<Menu> menus = new ArrayList<>();
                for (RoleMenu roleMenu : list1) {
                    Menu menu = menuDao.selectOne(new QueryWrapper<Menu>().eq("id", roleMenu.getMenuid()));
                    menus.add(menu);
                }
                role.setMenus(menus);
                user1.setRole(role);
            }
        }
        return list;

    }

//    @Override
//    public List<User> selectThree(User user) {
//        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//        List<User> list=userDao.selectList(userQueryWrapper);
//        for (User user1 : list) {
//
//        }
//    }

//
//    @Override
//    public User findUserByName(User user) {
//        return userDao.selectOne(new QueryWrapper<User>().eq("username", user.getUsername()));
//    }
}
